// 通用响应接口
export interface CommonResponse {
  code: number // 0表示成功，其余表示失败
  msg: string // 提示信息
}

export interface Data {
  ID: number
  content: string
  isFinished: boolean
}

interface ListResponse extends CommonResponse {
  data: {
    todo_list: Data[]
    total: number
    history_total: number
  }
}

interface AddResponse extends CommonResponse {
  data: Data
}

// 获取事项列表
export function reqTodoList(): Promise<ListResponse> {
  return fetch('api/todo', {
    method: 'get',
  }).then((res) => res.json())
}

//添加事项
export function reqAddTodo(Content: String): Promise<AddResponse> {
  return fetch('api/todo', {
    method: 'post',
    body: JSON.stringify({
      content: Content,
    }),
    headers: {
      'Content-Type': 'application/json',
    },
  }).then((res) => res.json())
}

//删除事项
export function reqDeleteTodo(ID: number): Promise<CommonResponse> {
  return fetch('api/todo', {
    method: 'delete',
    body: JSON.stringify({
      id: ID,
    }),
    headers: {
      'Content-Type': 'application/json',
    },
  }).then((res) => res.json())
}

//修改事项
export function reqUpdateTodo(ID: number): Promise<CommonResponse> {
  return fetch('api/todo', {
    method: 'put',
    body: JSON.stringify({
      id: ID,
    }),
    headers: {
      'Content-Type': 'application/json',
    },
  }).then((res) => res.json())
}
